﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace NexTrackDesktop.Controls
{
    /// <summary>
    /// THIS IS A PROTOTYPE OF THE FACEBOOK LOGIN DIALOG.
    /// 
    /// UNUSED AS OF 3/21/13
    /// </summary>
    public partial class FacebookLogin : UserControl
    {
        //The client_id here is a simple test facebook app
        private const string FB_LOGIN_URL =
            "https://www.facebook.com/dialog/oauth?" +
            "client_id=444645295616928" +
            "&redirect_uri=https://www.facebook.com/connect/login_success.html" +
            "&display=popup" +
            "&response_type=token";

        public bool LoggedIn
        {
            get { return (bool)GetValue(LoggedInProperty); }
            set { SetValue(LoggedInProperty, value); }
        }

        public bool Loading
        {
            get { return (bool)GetValue(LoadingProperty); }
            set { SetValue(LoadingProperty, value); }
        }

        public string UserID
        {
            get { return (string)GetValue(UserIDProperty); }
            set { SetValue(UserIDProperty, value); }
        }

        public string AccessToken
        {
            get { return (string)GetValue(AccessTokenProperty); }
            set { SetValue(AccessTokenProperty, value); }
        }

        #region DependencyProperties

        // Using a DependencyProperty as the backing store for AccessToken.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AccessTokenProperty =
            DependencyProperty.Register("AccessToken", typeof(string), typeof(FacebookLogin), new UIPropertyMetadata(null));


        // Using a DependencyProperty as the backing store for UserID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UserIDProperty =
            DependencyProperty.Register("UserID", typeof(string), typeof(FacebookLogin), new UIPropertyMetadata(null));


        // Using a DependencyProperty as the backing store for LoggedIn.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LoggedInProperty =
            DependencyProperty.Register("LoggedIn", typeof(bool), typeof(FacebookLogin), new UIPropertyMetadata(false));

        // Using a DependencyProperty as the backing store for Loading.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LoadingProperty =
            DependencyProperty.Register("Loading", typeof(bool), typeof(FacebookLogin), new UIPropertyMetadata(true));

        #endregion

        //We need to use the WinForms web browser to get more functionality...
        //WebBrowser loginBrowser;

        public FacebookLogin()
        {
            // 3/21/2013
            throw new NotImplementedException("Error: The Facebook Login was deprecated on 3/21/2013");

            InitializeComponent();
            this.DataContext = this;

            //loginBrowser = new WebBrowser();
            //loginBrowser.ScriptErrorsSuppressed = true;
            //loginBrowser.IsWebBrowserContextMenuEnabled = false;
            loginBrowser.Navigating +=new NavigatingCancelEventHandler(loginBrowser_Navigating);
            loginBrowser.Source = new Uri(FB_LOGIN_URL);
            //loginBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
            //formsHost.Coi = loginBrowser;

        }

        private void loginBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (e.Uri.GetLeftPart(UriPartial.Path) == "https://www.facebook.com/dialog/oauth")
                return;

            if (e.Uri.GetLeftPart(UriPartial.Path) == "https://www.facebook.com/connect/login_success.html")
            {
                Loading = false;
                LoggedIn = true;
                loginBrowser.Visibility = Visibility.Hidden;
                if (LoginCompleted != null)
                    LoginCompleted();
            }
            else
            {
                Loading = false;
                LoggedIn = false;
            }
        }

        public event Action LoginCompleted;
    }
}
